import Vue from 'vue'
import VueRouter from 'vue-router'
import MusicHall from '../views/MusicHall.vue'
import Lastmusic from '../views/Lastmusic.vue'
import Mymusic from '../views/Mymusic.vue'
import Radio from '../views/Radio.vue'
import Recommend from '../views/Recommend.vue'
import SearchList from '../views/SearchList.vue'
import SongList from '../views/SongList.vue'
import Likemusic from '../views/Likemusic.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/MusicHall' },
  { path: '/MusicHall', component: MusicHall },
  { path: '/lastmusic', component: Lastmusic },
  { path: '/mymusic', component: Mymusic },
  { path: '/radio', component: Radio },
  { path: '/recommend', component: Recommend },
  { path: '/searchList', component: SearchList },
  { path: '/songList', component: SongList },
  { path: '/likemusic', component: Likemusic }
]

// 挂载路由导航守卫

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  //     next()  放行    next('/login')  强制跳转

  if (to.path !== '/recommend') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/MusicHall')
  next()
})
export default router
